package com.guxingke.gblog.service.impl;

import com.guxingke.gblog.dao.UserDao;
import com.guxingke.gblog.dao.impl.UserDaoImpl;
import com.guxingke.gblog.dmo.User;
import com.guxingke.gblog.dto.UserDto;
import com.guxingke.gblog.service.UserService;
import com.guxingke.gblog.util.MD5Util;
import com.guxingke.gblog.util.RegexpCheckUtils;

public class UserServiceImpl implements UserService {
	private UserDao userDao = null;

	public UserServiceImpl() {
		userDao = new UserDaoImpl();
	}

	@Override
	public UserDto login(User user) {

		UserDto result = null;
		try {
			user.setPassword(MD5Util.EncoderPwdByMd5(user.getPassword()));
			result = userDao.login(user);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	@Override
	public void saveUser(User user) {

		try {
			user.setNickname(RegexpCheckUtils.badFilter(RegexpCheckUtils.htmlRemoveTag(user.getNickname())));
			user.setUsername(RegexpCheckUtils.badFilter(RegexpCheckUtils.htmlRemoveTag(user.getUsername())));
			userDao.save(user);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	public void updateUser(User user) {

		try {
			userDao.update(user);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
